﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditLocation : AdminPageBase
    {
        protected List<ViandInfo> ViandList = new List<ViandInfo>();

        private LocationInfo _locationInfo = new LocationInfo();
        private readonly LocationController _locationController = new LocationController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["LocationId"]))
                _locationInfo = _locationController.GetLocation(int.Parse(Request.QueryString["LocationId"]));

            if (Page.IsPostBack) return;

            LoadAdminUser();
            LoadLocationType();
            LoadStatus();
            LoadAddressDefault();
            LoadContinent();

            ViandList = new ViandController().ListViand().OrderBy(p => p.ViandName).ToList();

            if (_locationInfo.LocationId == -1) return;

            LoadNational();
            LoadCity();
            LoadDistrict();
            LoadWard();

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListLocation.aspx?xml=Location");
        }

        private void SaveData()
        {
            _locationInfo.LocationName = txtLocationName.Text;
            _locationInfo.LocationTypeId = int.Parse(ddlLocationType.SelectedValue);
            _locationInfo.ContinentId = int.Parse(ddlContinent.SelectedValue);
            _locationInfo.NationalId = int.Parse(Request["ctl00$placeHolderContent$ddlNational"]);
            _locationInfo.CityId = int.Parse(Request["ctl00$placeHolderContent$ddlCity"]);
            _locationInfo.DistrictId = int.Parse(Request["ctl00$placeHolderContent$ddlDistrict"]);
            _locationInfo.WardId = int.Parse(Request["ctl00$placeHolderContent$ddlWard"]);
            _locationInfo.Address = txtAddress.Text;
            _locationInfo.SortDescription = txtSortDescription.Text;
            _locationInfo.DetailDescription = txtContent.Text;
            _locationInfo.Status = int.Parse(ddlStatus.SelectedValue);
            _locationInfo.Code = txtCode.Text;

            if (_locationInfo.LocationId == -1)
            {
                _locationInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _locationInfo.CreatedDate = DateTime.Now;
                _locationController.InsertLocation(_locationInfo);
            }
            else
            {
                _locationInfo.UpdatedBy = UserId;
                _locationInfo.UpdatedDate = DateTime.Now;
                _locationController.UpdateLocation(_locationInfo);
            }
            Response.Redirect("/Pages/ListLocation.aspx?xml=Location");
        }

        private void RenderForm()
        {
            txtLocationName.Text = _locationInfo.LocationName;
            ddlLocationType.SelectedValue = _locationInfo.LocationTypeId.ToString();
            ddlContinent.SelectedValue = _locationInfo.ContinentId.ToString();
            ddlNational.SelectedValue = _locationInfo.NationalId.ToString();
            ddlCity.SelectedValue = _locationInfo.CityId.ToString();
            ddlDistrict.SelectedValue = _locationInfo.DistrictId.ToString();
            ddlWard.SelectedValue = _locationInfo.WardId.ToString();
            txtAddress.Text = _locationInfo.Address;
            txtSortDescription.Text = _locationInfo.SortDescription;
            txtContent.Text = _locationInfo.DetailDescription;
            ddlStatus.SelectedValue = _locationInfo.Status.ToString();
            ddlAdminUser.SelectedValue = _locationInfo.CreatedBy.ToString();
            txtCode.Text = _locationInfo.Code;
        }

        private void LoadLocationType()
        {
            ddlLocationType.DataSource = new LocationTypeController().ListLocationType()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.LocationTypeName);
            ddlLocationType.DataTextField = "LocationTypeName";
            ddlLocationType.DataValueField = "LocationTypeId";
            ddlLocationType.DataBind();

            ddlLocationType.Items.Insert(0, new ListItem("--- Chọn loại địa điểm ---", "-1"));
        }
        private void LoadAddressDefault()
        {
            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }
        private void LoadContinent()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Continent.xml"));
            var dv = ds.Tables["Continent"].DefaultView;
            ddlContinent.DataTextField = "Name";
            ddlContinent.DataValueField = "ID";
            ddlContinent.DataSource = dv;
            ddlContinent.DataBind();

            ddlContinent.Items.Insert(0, new ListItem("--- Chọn châu lục ---", "-1"));
        }
        private void LoadNational()
        {
            ddlNational.DataSource = new NationalController().ListNationalByContinent(_locationInfo.ContinentId);
            ddlNational.DataTextField = "NationalName";
            ddlNational.DataValueField = "NationalId";
            ddlNational.DataBind();

            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
        }
        private void LoadCity()
        {
            ddlCity.DataSource = new CityController().ListCityByNational(_locationInfo.NationalId);
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityId";
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
        }
        private void LoadDistrict()
        {
            ddlDistrict.DataSource = new DistrictController().ListDistrictByCity(_locationInfo.CityId);
            ddlDistrict.DataTextField = "DistrictName";
            ddlDistrict.DataValueField = "DistrictId";
            ddlDistrict.DataBind();

            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
        }
        private void LoadWard()
        {
            ddlWard.DataSource = new WardController().ListWardByDistrict(_locationInfo.DistrictId);
            ddlWard.DataTextField = "WardName";
            ddlWard.DataValueField = "WardId";
            ddlWard.DataBind();

            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }
        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}